"use strict";

const { Controller } = require("egg");

class InfoController extends Controller {
  // 获取资讯数据
  async getInfo() {
    const { ctx } = this;
    const { type, value } = ctx.query;
    const list = await this.app.mysql.select("info");
    ctx.body = {
      code: 200,
      list: list
        .filter((v) => {
          if (type === "全部") {
            return true;
          }
          return v.state === type;
        })
        .filter((v) => {
          if (value == "") {
            return true;
          }
          return v.name.includes(value);
        }),
    };
  }


    // 新增资讯
    async addInfo() {
        const { ctx } = this;
        const { title,name } = ctx.request.body;
        const res = await this.app.mysql.insert("info", { title,name,date:new Date().toLocaleString('SE-sv') });
        if (res.affectedRows === 1) {
          ctx.body = {
            code: 200,
          };
        } else {
          ctx.body = {
            code: 300,
          };
        }
       
      }
         // 编辑资讯
    async editInfo() {
        const { ctx } = this;
        const { title,name,key,state} = ctx.request.body;
        const res = await this.app.mysql.update("info", { title,name,state,date:new Date().toLocaleString() },{where:{key}});
        if (res.affectedRows === 1) {
          ctx.body = {
            code: 200,
          };
        } else {
          ctx.body = {
            code: 300,
          };
        }
       
      }
}

module.exports = InfoController;
